/******************************************************************************
This .do file produces the results reported in Figures OB1 & OB2. 
The figures were produced in R using estimated saved 
from these analyses. 
******************************************************************************/

clear
use "ess_cleaned_whole.dta"
set more off

*Restrict to citizens
keep if citizen == 1

/*********************************
Weights
*********************************/

gen anweight = pspwght*pweight
label var anweight "Analysis Weight"
*Note: svyset is not used because mixed does not work with svy: in this version of Stata. 

/*********************************
Labelling
*********************************/

label var self_te "Self Transcendence vs. Self-Enhancement"
label var cons_open "Conservation vs. Openness"
label var age "Age"
label var gender "Gender"
label def gend 1 "Female" 0 "Male"
label values gender gend

recode born_when (1=1 "Born in Country") (2 3 4 = 2 "Came to Country <20 Yrs Ago") (5 = 3 "Came to Country 20+ Yrs Ago"), gen(born_when1)
label var born_when1 "Born in Country?"

recode minority (0=1 "Minority Group") (1=0 "Non-Minority"), gen(minority1)
label var minority1 "Member of Minority?"

recode edulvla (55=.)
label var edulvla "Own Education"
label def ed 0 "Other Ed Level"  1 "< Lower Secondary" 2 "Lower Secondary" 3 "Upper Secondary" 4 "Post-Secondary, Non-Tertiary" 5 "Tertiary Education"
label values edulvla ed

recode edulvlfa (55=.)
label var edulvlfa "Father's Education"
label val edulvlfa ed

recode union (1 = 0 "Never a Union Member") (2 3 = 1 "Currently/Previously in Union"), gen(union1)
label var union1 "Union Membership"

label var socialtrust "Interpersonal Trust"
label var sclmeet "Freq of Social Interactions"
label var religiosity "Religiosity"
label var yrlvdae "Residential Stability"

label def dom 1 "Big City" 2 "Suburbs" 3 "Small City" 4 "Country Village" 5 "Countryside" 
label values domicil dom

label var post_soviet "Socialist Country?"
label def pos 1 "Socialist Country" 0 "Non-Socialist Country"
label values post_soviet pos

label var demyrs_01 "Years of Democracy"
label var ethnic_01 "Ethnic Heterogeneity"

/*********************************
Values Scales
*********************************/

*ST
alpha  care_rev  enviro_rev equally_rev  loyal_rev  understand_rev
egen transcend = rowmean(care_rev  enviro_rev equally_rev  loyal_rev  understand_rev)

*SE
alpha admired_rev  respect_rev  rich_rev successful_rev
egen enhance = rowmean(admired_rev  respect_rev  rich_rev successful_rev)

*Cons
alpha behave_rev followrules_rev  modest_rev safe_rev  stronggov_rev  tradition_rev
egen conserve = rowmean(behave_rev followrules_rev  modest_rev safe_rev  stronggov_rev  tradition_rev) 

*Open
alpha adventures_rev  befree_rev creative_rev fun_rev goodtime_rev  trynew_rev
egen open = rowmean(adventures_rev  befree_rev creative_rev fun_rev goodtime_rev  trynew_rev)

**Relative

gen self_relative = transcend - enhance
gen cons_relative = conserve - open 

label var self_relative "Self-Transcendence vs. Self-Enhancement"
label var cons_relative "Conservation vs. Openness"


/*********************************
Rescaling Continuous Variables to 0-1 Scale
*********************************/

foreach var in support_others vote_elections obey opinions volunteer beactive self_relative cons_relative age socialtrust sclmeet religiosity yrlvdae {
	summ `var'
	replace `var' = (`var' - r(min))/(r(max)-r(min))
}

/*********************************
Models, Table, Saved Results
*********************************/

foreach var in vote_elections obey support_others opinions volunteer beactive  {
	local m2 " age i.gender i.minority1 i.union1 i.edulvla ib1.edulvlfa i.born_when1 i.post_soviet demyrs_01 ethnic_01"
	local m3 " age i.gender i.minority1 i.union1 i.edulvla ib1.edulvlfa i.born_when1 socialtrust sclmeet religiosity yrlvdae i.post_soviet demyrs_01 ethnic_01 "
	eststo clear
	eststo: mixed `var' self_relative cons_relative [pweight=anweight] || country: self_relative cons_relative,  stddev
		parmest, label format(estimate min95 max95 p) list(parm label estimate min95 max95 p) saving(`var'1_rel.dta, replace)
	eststo: mixed `var' self_relative cons_relative `m2' [pweight=anweight] || country: self_relative cons_relative,  stddev
		parmest, label format(estimate min95 max95 p) list(parm label estimate min95 max95 p) saving(`var'2_rel.dta, replace)
	eststo: mixed `var' self_relative cons_relative `m3' [pweight=anweight] || country: self_relative cons_relative,  stddev
		parmest, label format(estimate min95 max95 p) list(parm label estimate min95 max95 p) saving(`var'3_rel.dta, replace)
	esttab using `var'_rel.rtf, replace label se b(2) aic bic onecell transform(ln*: exp(@) exp(@)) ///
		addnote("Models use survey weights. Value orientation slopes are allowed to vary across countries. All continuous variables are scaled to range from 0-1.") ///
		mtitles("Model 1" "Model 2" "Model 3") ///
		title("{\b Table X}: Difference Score Value Orientations and `var'") ///
	order(self_relative cons_relative age 0.gender 1.gender 0.minority1 1.minority1 0.union1 1.union1 1.edulvla 2.edulvla 3.edulvla 4.edulvla 5.edulvla ///
		1.edulvlfa 2.edulvlfa 3.edulvlfa 4.edulvlfa 5.edulvlfa 0.edulvlfa 1.born_when1 2.born_when1 3.born_when1 socialtrust sclmeet religiosity yrlvdae 0.post_soviet 1.post_sovet demyrs_01 ethnic_01)
	}
